package com.xj.demo.customxmltag;

import com.xj.demo.bean.UserBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
	private static final String NAME_ATTRIBUTE = "name";
	private static final String AGE_ATTRIBUTE = "age";
	@Override
	protected Class<?> getBeanClass(Element element) {
		return UserBean.class;
	}

	@Override
	protected String getBeanClassName(Element element) {
		return UserBean.class.getName();
	}

	@Override
	protected void doParse(Element element, BeanDefinitionBuilder builder) {
		String name = element.getAttribute(NAME_ATTRIBUTE);
		if (StringUtils.hasLength(name)) {
			builder.addPropertyValue(NAME_ATTRIBUTE, name);
		}
		String ageStr = element.getAttribute(AGE_ATTRIBUTE);
		if (StringUtils.hasLength(ageStr)) {
			Integer age = Integer.valueOf(ageStr);
			builder.addPropertyValue(AGE_ATTRIBUTE, age);
		}
	}
}
